package dummy.p1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;

public class A {

	String mm(Number m) throws Error {
		System.out.println("a.mm(Number)");
		return "";
	}

	void mm(Object m) {
		System.out.println("a.mm(Object)");
	}

	static class MyLoader extends URLClassLoader {
		static String dir = "file:/C:/workspace/Algorihms/ffalg/bin/";

		public MyLoader() throws Exception {
			super(new URL[] { new URL(dir) });
		}

		@Override
		protected synchronized Class<?> loadClass(String name, boolean resolve)
				throws ClassNotFoundException {
			{
				// First, check if the class has already been loaded
				Class c = findLoadedClass(name);
				if (c == null) {
					c = findClass(name);

					try {
						c = findClass(name);
					} catch (ClassNotFoundException e) {
						c = super.loadClass(name, false);
					}					

				}
				if (resolve) {
					resolveClass(c);
				}
				return c;
			}
		}

	}

	public static void main(String[] args) throws Exception {
		new YY();
		ClassLoader cl = ClassLoader.getSystemClassLoader();
		// Class implClass = Class.forName(Impl.class.getName(), false,
		// Thread.currentThread().getContextClassLoader());

		System.out.println(cl);
		Thread t1 = new R(1);
		Thread t2 = new R(2);
		URL url = new URL("file:/C:/workspace/Algorihms/ffalg/bin/");
		t1.setContextClassLoader(new MyLoader());
		t2.setContextClassLoader(new MyLoader());
		t1.start();
		t2.start();
	}
}

class R extends Thread {
	int x;

	public R(int x) {
		this.x = x;
	}

	@Override
	public void run() {
		synchronized (R.class) {
			System.out.println("-------------------------");
			System.out.println(Thread.currentThread().toString());
			int x = Impl.x;
			System.out.println(Impl.x);
			Impl.x = this.x;
			System.out.println(Impl.x);
			System.out.println("-------------------------");
		}
	}
}

class XX {
	static YY t;
	static {
		t = new YY();
		System.out.println("static XX");
	}
	{
		System.out.println("init XX");
	}

	public XX() {
		System.out.println("XX()");
	}
}

class YY extends XX {
	final static int t;
	static {
		System.out.println("static YY");
		t = 1;
	}
	{
		System.out.println("init YY");
	}

	public YY() {
		System.out.println("YY():" + t);
	}
}

class Impl {
	static int x = -1;
	static {
		System.out.println("Initialized by thread" + Thread.currentThread());
	}
}